package controller.priv;

import controller.MyCoolServlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import model.User;
import persistence.user.UserDAO;
import persistence.user.UserPersistFactory;

public class DeleteUserServlet extends MyCoolServlet {

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        UserDAO persistenceManager = UserPersistFactory.getUserDAO(persistenceMechanism);
        HttpSession s = request.getSession();
        User user = (User) s.getAttribute("usuario");
        String name = user.getUsuario();
        if (persistenceManager.deleteUser(name)) {
            user.setUsuario(null);
            s.setAttribute("usuario", user);
            request.setAttribute("message",name + " ha sido eliminado de appBuy");
            gotoURL(successForm, request, response);
        } else {
            request.setAttribute("message1", "Error al eliminar usuario:");
            request.setAttribute("message2", "Vuelva a intentarlo de nuevo con posterioridad");
            gotoURL(errorForm, request, response);
        }
    }
}